﻿namespace ClassLibrary.DataLayer.DataObjects
{
    using ClassLibrary.BusinessLayer.BusinessObjects;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;

    public class ProjectGroupDAO : IProjectGroupDAO
    {
        public bool DeleteProjectGroup(ProjectGroup clsProjectGroup)
        {
            return clsProjectGroup.Save("D");
        }

        public IList<ProjectGroup> GetProjectGroup(ProjectGroup clsProjectGroup)
        {
            DataTable dt = Db.GetDataTable(clsProjectGroup.CreateGetCommandParameters());
            clsProjectGroup.LoadDataToCollection(dt.Rows[0]);
            if (dt.Rows[0] == null)
            {
                throw new Exception("ProjectGroup doesn't exist");
            }
            IList<ProjectGroup> list = new List<ProjectGroup>();
            list.Add(clsProjectGroup);
            return list;
        }

        public IList<ProjectGroup> GetProjectGroup(string projectGroupId)
        {
            ProjectGroup clsProjectGroup = new ProjectGroup();
            clsProjectGroup.ProjectGroupId = projectGroupId;
            return this.GetProjectGroup(clsProjectGroup);
        }

        public IList<ProjectGroup> GetsProjectGroup()
        {
            ProjectGroup clsProjectGroup = new ProjectGroup();
            IDataReader dtReader = Db.GetDataReader(clsProjectGroup.CreateGetsCommandParameters());
            IList<ProjectGroup> list = new List<ProjectGroup>();
            while (dtReader.Read())
            {
                clsProjectGroup = new ProjectGroup();
                clsProjectGroup.LoadDataToCollection(dtReader);
                list.Add(clsProjectGroup);
            }
            return list;
        }

        public DataTable GetsProjectGroup(string SPName, DbParameter[] param)
        {
            ProjectGroup clsProjectGroup = new ProjectGroup();
            return Db.GetDataTable(clsProjectGroup.GetsDataReader(SPName, param));
        }

        public bool InsertProjectGroup(ProjectGroup clsProjectGroup)
        {
            return clsProjectGroup.Save("I");
        }

        public int InsertProjectGroupReturnIdentity(ProjectGroup clsProjectGroup)
        {
            return clsProjectGroup.Save("I", true);
        }

        public string InsertProjectGroupReturnOutput(ProjectGroup clsProjectGroup)
        {
            return clsProjectGroup.Save("I", "@ProjectGroupID");
        }

        public IList<ProjectGroup> ListProjectGroup(ProjectGroup clsProjectGroup)
        {
            IDataReader dtReader = Db.GetDataReader(clsProjectGroup.CreateListCommandParameters());
            IList<ProjectGroup> list = new List<ProjectGroup>();
            while (dtReader.Read())
            {
                ProjectGroup clsProjectGroups = new ProjectGroup();
                clsProjectGroups.LoadDataToCollection(dtReader);
                list.Add(clsProjectGroups);
            }
            return list;
        }

        public IList<ProjectGroup> ListProjectGroup(string projectGroupId)
        {
            ProjectGroup clsProjectGroup = new ProjectGroup();
            clsProjectGroup.ProjectGroupId = projectGroupId;
            return this.ListProjectGroup(clsProjectGroup);
        }

        public bool UpdateProjectGroup(ProjectGroup clsProjectGroup)
        {
            return clsProjectGroup.Save("U");
        }

        public int UpdateProjectGroup(string SPName, DbParameter[] param)
        {
            ProjectGroup clsProjectGroup = new ProjectGroup();
            return Db.ExecuteNonQuery(clsProjectGroup.UpdateQuery(SPName, param));
        }
    }
}

